package com.adcc.swingui.util;

import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;


/**
 * <p>Title: ResUtil</p>
 * <p>Description:资源管理器 </p>
 * <p>Copyright: Copyright (c) 2010</p>
 * @author jeff.zhao
 * @version 1.0.0
 */
public class ResUtil
{
    public static final String CATEGORY_EXCEPTION = "exception.errinfo_" ;
    public static final String CATEGORY_MESSAGE = "message.msginfo_" ;
    public static final String CATEGORY_MENU = "menu.menuinfo_" ;
    protected static Locale currLocale ;
    private static Hashtable< String , ResourceBundle > resTable = new Hashtable<String, ResourceBundle>() ;
    
    /**私有构造*/
    private ResUtil()
    {
    }
    
    
    public static void setDefaultLocale( Locale loc )
    {
    	currLocale = loc ;
    	clearResourcePool() ;
    }
    
    private static void clearResourcePool()
    {
    	resTable.clear() ;
    }

    /**获取本地资异常配置信息
     * @param String errKey 异常键
     * @return 返回异常信息 
     * */
    public static String getExceptionString( String errKey )
    {
        Locale currLocale = Locale.getDefault();
        return ResUtil.getExceptionString( errKey , currLocale ) ;
    }
    
    /**获取本地资异常配置信息
     * @param String errKey 异常键
     * @return 返回异常信息 
     * */
    public static String getExceptionString( String errKey , String... params )
    {
        return ResUtil.getExceptionString( errKey , currLocale , params ) ;
    }
    
    /**按指定的Locale获取相应的异常配置信息*/
    public static String getExceptionString( String errKey , Locale _locale )
    {
    	return getExceptionString(  errKey ,  _locale , null ) ;
    }
    
    private static ResourceBundle getResourceBundle( String resPath )
    {
    	 ResourceBundle bundle = resTable.get( resPath ) ;
    	 
    	 if( bundle == null )
    	 {
    		 bundle = ResourceBundle.getBundle( resPath ) ;
    		 resTable.put( resPath , bundle ) ;
    	 }
    	 
    	 return bundle ;
    }
    
    public static String getExceptionString( String errKey , Locale _locale , String[] params )
    {
        StringBuffer errMsg = new StringBuffer( getString( errKey , _locale , CATEGORY_EXCEPTION  ) ) ;
        
        if( params != null )
        {
        	int i = 0 ;
        	int index = errMsg.indexOf( "{" )  ;
        	
        	while( index != -1 )
        	{
        		if( params.length != i )
        		{
        			int realIndex = Integer.parseInt( errMsg.substring( index + 1  , index  + 2  ) ) ;
        			errMsg.replace( index , index + 3 , params[ realIndex ] ) ;
        		}
        		else
        		{
        			break ;
        		}
        		
        		++ i ; 
        		index = errMsg.indexOf( "{" ) ;
        	}
        }
        
        return errMsg.toString() ;
    }

    public static String getMessageString( String msgKey )
    {
        return ResUtil.getMessageString( msgKey , currLocale ) ;
    }

    public static String getMessageString( String msgKey , Locale sLocale )
    {
        String msgMsg = msgKey ;
        msgMsg = getString( msgKey , sLocale , CATEGORY_MESSAGE ) ;
        return msgMsg ;
    }
    
    public static String getMenuString( String msgKey )
    {
        return ResUtil.getMenuString( msgKey , currLocale ) ;
    }
    
    private static String getString( String msgKey , Locale sLocale , String path )
    {
        String msgMsg = msgKey ;
        String resources = "config." + path  + ServiceLocaleMgr.getLocaleCode( sLocale ) ;
        try
        {
	        ResourceBundle _bundle = getResourceBundle( resources ) ;
	        String tmpStr = _bundle.getString( msgKey ) ;
	        msgMsg = tmpStr;
        }
        catch( Exception ex )
        {
        	//log.warn( "[U]没有为[" + msgKey + "]找到合适的资源信息") ;
        }

        return msgMsg ;
    }

    public static String getMenuString( String msgKey , Locale sLocale )
    {
        String msgMsg = msgKey ;
        msgMsg = getString( msgKey , sLocale , CATEGORY_MENU ) ;
        return msgMsg ;
    }
    
    public static void main( String[] args )
    {
//    	System.out.println( getMessageString( "ads.search.flight.title" ) );
//    	System.out.println( getMenuString( "ads.menu.alarmflag" ) );
    	System.out.println( getExceptionString( "THREAD0001" , "aaa"  ) );
    }
}
